package customfields;

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;

public class GradeField extends Field{

	private String text;
	private String grade="";
	private int height;
	private int width;
	private int padding = 10;
	private int textColorUnfocused = Color.BLACK;
	private int textColorFocused = Color.WHITE;
	private int textColor;

	public GradeField(String activity, String grade, long style){
		super(style);
		this.text = activity;
		this.grade=grade;
		height = (int) (getFont().getHeight()*1.5);  
		width = (int) (Display.getWidth()*0.75); 
	}

	public GradeField(String txt, long style){
		super(style);
		this.text = txt;
		height = (int) (getFont().getHeight()*1.5);  
		width = (int) (Display.getWidth()*0.75); 
	}

	public GradeField(String txt, long style, int w, int h){
		super(style);
		this.text = txt;
		height = w;  
		width = h; 
	}

	public GradeField(String txt){
		super();
		this.text=txt;
		height = (int) (getFont().getHeight()*1.5);  
		width = (int) (Display.getWidth()*0.75); 
	}

	public int getPreferredWidth() {  
		return width;  
	}  

	public int getPreferredHeight() {  
		return height;  
	}  

	protected void onFocus(int direction) {
		textColor = textColorFocused; 
		invalidate();  
	}  

	protected void onUnfocus() {  
		textColor = textColorUnfocused;  
		invalidate();  
	} 

	protected void layout(int arg0, int arg1) {  
		setExtent(getPreferredWidth(), getPreferredHeight());  
	}  

	protected void paint(Graphics graphics) {
		// draw text: left aligned
		graphics.setColor(textColor);
		if(getFont().getAdvance(text)>width){
			text = dot(text);
		}
		graphics.drawText(text,padding,padding/2);
		// draw the grade: right aligned
		graphics.drawText(grade, getWidth()-getFont().getAdvance(grade)-padding, padding/2);
	}

	private String dot(String word){
		word = word.substring(0,word.length()-1);
		while (getFont().getAdvance(word+" (...)")>(width-padding)){
			word = word.substring(0,word.length()-1);
		}
		return word+" (...)";
	}

	protected boolean navigationClick(int status, int time){
		return true;
	}
	protected boolean navigationUnclick(int status, int time){
		return true;
	}
}
